package com.dazhewan.project.component;

import com.dazhewan.project.component.menubar.MyMenuBar;
import com.dazhewan.project.properties.ContainerSize;
import com.dazhewan.project.properties.GlobalListener;
import com.dazhewan.project.properties.GlobalSettting;
import com.dazhewan.project.properties.ScreenInfoCache;
import com.dazhewan.utils.FunctionUtil;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author: zheng
 * @code date: 2022/10/10
 * @code className: CommontMain
 * @code description: 主界面
 */
public class CommontMain extends AbstractMainBuilder {
    private JFrame mainContainer = new JFrame();
    public JPanel panel = new JPanel();
    public static final JTextArea textArea = new JTextArea();

    @Override
    public JFrame mainFrame() {
        return mainContainer;
    }

    public CommontMain() {
        GlobalSettting.MAIN_BUILDER =this;
        try {
            ImageIcon imageIcon = new ImageIcon(FunctionUtil.getRootPath() + "\\icon\\title.png");
            mainContainer.setIconImage(imageIcon.getImage());
        } catch (IOException e) {
            e.printStackTrace();
        }
        FunctionUtil.addContainer(mainContainer.getContentPane());
        //初始化mainCOntainer
        setMainContainer();
        GlobalListener globalListener = new GlobalListener();
        //组装工具条
        mainContainer.setJMenuBar(new MyMenuBar(mainContainer).getMenuBar());
        //组装主区域
        mainContainer.add(getMainPanel());
        //组装底部区域
        mainContainer.add(getBottomPanl(), BorderLayout.SOUTH);
        //设置窗口可见
        mainContainer.setVisible(true);
    }

    private JPanel getBottomPanl() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        JLabel text = new JLabel(" @ 郑学学的ocr  version1.0");
        //设置字体
        text.setFont(new java.awt.Font("华文行楷", 1, 10));
        //设置颜色
        text.setForeground(Color.lightGray);
        text.setPreferredSize(new Dimension(mainContainer.getWidth(), 15));
        panel.add(text);
        return panel;
    }

    private JPanel canvas = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (ScreenInfoCache.init().isEmpty()) {
                try {
                    drawImage(g,new ImageIcon(FunctionUtil.getRootPath() + "\\icon\\main.png").getImage(), canvas);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                final BufferedImage image = ScreenInfoCache.init().getCutScreenInfo().getImage();
                drawImage(g,image,canvas);
            }
        }
    };

    public void drawImage(Graphics g, Image image, JPanel canvas) {
        final double withNum = FunctionUtil.getProportionNum(image.getWidth(canvas), canvas.getWidth());
        final double withHeight = FunctionUtil.getProportionNum(image.getHeight(canvas), canvas.getHeight());
        int width,height;
        //宽不超出
        if(withNum==image.getWidth(canvas)){
            //高也不超出
            if(withHeight==image.getHeight(canvas)){
                width=image.getWidth(canvas);
                height=image.getHeight(canvas);
            }else {
                //高超出,按高得比例进行缩放
                width= (int) (image.getWidth(canvas)*withHeight);
                height= (int) (image.getHeight(canvas)*withHeight);

            }
        }else {
            //宽超出，高不超出
            if(withHeight==image.getHeight(canvas)){
                //按宽得比例进行缩放
                width= (int) (image.getWidth(canvas)*withNum);
                height= (int) (image.getHeight(canvas)*withNum);
            }else {
               //高也超出，宽也超出按最小得比例进行缩放
                final double min = Math.min(withHeight, withNum);
                width= (int) (image.getWidth(canvas)*min);
                height= (int) (image.getHeight(canvas)*min);
            }
        }

        g.drawImage(image, (canvas.getWidth()-width)/2, (canvas.getHeight()-height)/2,width,height, null);
    }

    @Override
    public void updateCanvas() {
        canvas.repaint();
    }

    @Override
    public void updateTextContent(String content) {
        textArea.setText(content);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection(textArea.getText()),null);
    }

    private Component getMainPanel() {
        panel.setBackground(GlobalSettting.MAIN_BLACK_COLOR);
        panel.setLayout(new GridLayout(1, 2));
        panel.setPreferredSize(mainContainer.getPreferredSize());

        //图像区域
        final Dimension dimension = new Dimension((int) mainContainer.getPreferredSize().getWidth() / 2, (int) mainContainer.getPreferredSize().getHeight() / 2);
        updateCanvas();
        canvas.setBackground(GlobalSettting.MAIN_BLACK_COLOR);
        canvas.setPreferredSize(dimension);
        panel.add(canvas, 0);


        //正文区域
        final JPanel text = new JPanel();
        text.setBackground(GlobalSettting.MAIN_BLACK_COLOR);
        text.setBorder(BorderFactory.createTitledBorder(new LineBorder(Color.LIGHT_GRAY, 1), "翻译文本", TitledBorder.RIGHT, TitledBorder.BOTTOM, new Font("华文楷体", Font.BOLD, 14), Color.GRAY));
        text.setPreferredSize(new Dimension((int) mainContainer.getPreferredSize().getWidth() / 3, (int) mainContainer.getPreferredSize().getHeight() / 3));
        text.setLayout(new GridLayout(1, 1));
        text.add(CommontMain.textArea);
        panel.add(text, 1);

        return panel;
    }


    /**
     * @method: 设置mainContainer的大小等数值
     */
    private void setMainContainer() {
        //设置位置
        mainContainer.setLocation(ContainerSize.getContainerSize().getLocalx(), ContainerSize.getContainerSize().getLocaly());
        mainContainer.setSize(ContainerSize.getContainerSize().getWidth(), ContainerSize.getContainerSize().getHeight());
    }
}
